package com.samehope.ar.controller;


import com.samehope.ar.common.CommonResult;
import com.samehope.ar.dto.RouteAndParam;
import com.samehope.ar.dto.RouteUpdateParam;
import com.samehope.ar.service.IPatrolRouteService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 巡检路线 前端控制器
 * </p>
 *
 * @author Zhang Luo
 * @since 2019-12-23
 */
@RestController
@RequestMapping("/route")
public class PatrolRouteController {

    @Autowired
    private IPatrolRouteService routeService;

    /**
     * 添加线路
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PostMapping("/add")
    public CommonResult add(@Valid @RequestBody RouteAndParam param) {
        routeService.add(param);
        return CommonResult.success(null);
    }

    /**
     * 更新线路
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @PutMapping("/update")
    public CommonResult update(@Valid @RequestBody RouteUpdateParam param) {
        routeService.update(param);
        return CommonResult.success(null);
    }

    /**
     * 删除线路
     * @param routeId
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @DeleteMapping("/delete/{routeId}")
    public CommonResult delete(@PathVariable("routeId") Long routeId) {
        routeService.deleteRoute(routeId);
        return CommonResult.success(null);
    }

    /**
     * 查询线路树结构
     * @param condition
     * @return
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/tree")
    public CommonResult tree(@RequestParam(required = false) String condition) {
        return CommonResult.success(routeService.getRouteTree(condition));
    }

    /**
     * 线路详情
     */
    @PreAuthorize("hasRole('ROLE_PROJECT')")
    @GetMapping("/detail/{routeId}")
    public CommonResult detail(@PathVariable("routeId") Long routeId) {
        return CommonResult.success(routeService.detail(routeId));
    }
}
